Conditions | 14 |
Paths | > 20000 |
Total Lines | 102 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like index.js ➔ pathtoRegexp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
28 | function pathtoRegexp(path, keys, options) { |
||
29 | options = options || {}; |
||
30 | keys = keys || []; |
||
31 | var strict = options.strict; |
||
32 | var end = options.end !== false; |
||
33 | var flags = options.sensitive ? '' : 'i'; |
||
34 | var extraOffset = 0; |
||
35 | var keysOffset = keys.length; |
||
36 | var i = 0; |
||
37 | var name = 0; |
||
38 | var m; |
||
39 | |||
40 | if (path instanceof RegExp) { |
||
41 | while (m = MATCHING_GROUP_REGEXP.exec(path.source)) { |
||
42 | keys.push({ |
||
43 | name: name++, |
||
44 | optional: false, |
||
45 | offset: m.index |
||
46 | }); |
||
47 | } |
||
48 | |||
49 | return path; |
||
50 | } |
||
51 | |||
52 | if (Array.isArray(path)) { |
||
53 | // Map array parts into regexps and return their source. We also pass |
||
54 | // the same keys and options instance into every generation to get |
||
55 | // consistent matching groups before we join the sources together. |
||
56 | path = path.map(function (value) { |
||
57 | return pathtoRegexp(value, keys, options).source; |
||
58 | }); |
||
59 | |||
60 | return new RegExp('(?:' + path.join('|') + ')', flags); |
||
61 | } |
||
62 | |||
63 | path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?')) |
||
64 | .replace(/\/\(/g, '/(?:') |
||
65 | .replace(/([\/\.])/g, '\\$1') |
||
66 | .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional, offset) { |
||
67 | slash = slash || ''; |
||
68 | format = format || ''; |
||
69 | capture = capture || '([^\\/' + format + ']+?)'; |
||
70 | optional = optional || ''; |
||
71 | |||
72 | keys.push({ |
||
73 | name: key, |
||
74 | optional: !!optional, |
||
75 | offset: offset + extraOffset |
||
76 | }); |
||
77 | |||
78 | var result = '' |
||
79 | + (optional ? '' : slash) |
||
80 | + '(?:' |
||
81 | + format + (optional ? slash : '') + capture |
||
82 | + (star ? '((?:[\\/' + format + '].+?)?)' : '') |
||
83 | + ')' |
||
84 | + optional; |
||
85 | |||
86 | extraOffset += result.length - match.length; |
||
87 | |||
88 | return result; |
||
89 | }) |
||
90 | .replace(/\*/g, function (star, index) { |
||
91 | var len = keys.length |
||
92 | |||
93 | while (len-- > keysOffset && keys[len].offset > index) { |
||
94 | keys[len].offset += 3; // Replacement length minus asterisk length. |
||
95 | } |
||
96 | |||
97 | return '(.*)'; |
||
98 | }); |
||
99 | |||
100 | // This is a workaround for handling unnamed matching groups. |
||
101 | while (m = MATCHING_GROUP_REGEXP.exec(path)) { |
||
102 | var escapeCount = 0; |
||
103 | var index = m.index; |
||
104 | |||
105 | while (path.charAt(--index) === '\\') { |
||
106 | escapeCount++; |
||
107 | } |
||
108 | |||
109 | // It's possible to escape the bracket. |
||
110 | if (escapeCount % 2 === 1) { |
||
111 | continue; |
||
112 | } |
||
113 | |||
114 | if (keysOffset + i === keys.length || keys[keysOffset + i].offset > m.index) { |
||
115 | keys.splice(keysOffset + i, 0, { |
||
116 | name: name++, // Unnamed matching groups must be consistently linear. |
||
117 | optional: false, |
||
118 | offset: m.index |
||
119 | }); |
||
120 | } |
||
121 | |||
122 | i++; |
||
123 | } |
||
124 | |||
125 | // If the path is non-ending, match until the end or a slash. |
||
126 | path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)')); |
||
127 | |||
128 | return new RegExp(path, flags); |
||
129 | }; |
||
130 |